"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具版本信息
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.dsscmd工具正常显示版本
    2.dsscmd工具使用异常参数-V，--Version等
Expect      :
    1.成功
    2.合理报错，但会显示帮助信息
History     :
"""
import os
import re
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:dsscmd工具正常显示版本;expect:成功-----"
        self.logger.info(step1)
        cmd_list = ['-v', '--version']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            exp = f"dsscmd \\(openGauss build .*\\) compiled at .* Release"
            search_res = re.search(exp, execute_msg, re.S)
            self.assertIsNotNone(search_res, '执行失败' + step1)

        step2 = "-----step2:dsscmd工具使用异常参数-V，--Version等;" \
                "expect:合理报错，但会显示帮助信息-----"
        self.logger.info(step2)
        cmd_list = ['-V', '--Version']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(f"cmd:{cmd} can not find.\n"
                          f"Usage:dsscmd [command] [OPTIONS]",
                          execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
